<template>
  <div>
    <el-dialog title="新增角色" :visible.sync="dialogFormVisible" :before-close="close" :close-on-click-modal="false">
      <el-form :model="form">
        <el-form-item label="角色名称" :label-width="formLabelWidth">
          <el-input v-model="form.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="描述" :label-width="formLabelWidth">
          <el-input  
            type="textarea"  
            v-model="form.description"
            :autosize="{ minRows: 2, maxRows: 4}"
            placeholder="请输入内容" >
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="close">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { createRole, checkRoleDetail, reviseRole } from 'api/system'
export default {
  props: ['id'],
  data() {
    return {
      dialogFormVisible: true,
      form: {
        creator: '小周',
        description: '',
        name: '',
        roleType: 'NORMAL'
      },
      formLabelWidth: '120px'
    }
  },
  created() {
    if (this.id) {
      this.checkDetail()
    }
  },
  methods: {
    close() {
      this.$emit('close')
    },
    async submit() {
      if (this.id) {
        this.edit()
        return
      }
      try {
        let back = await createRole(this.form)
        back = back.data
        if (back.resultCode === '200') {
          this.$emit('update')
          this.close()
        } else {
          this.$alert(back.msg, '提示')
        }
      } catch (err) {
        this.$alert('请求出错', '提示')
      }
    },
    async checkDetail() {
      try {
        let back = await checkRoleDetail(this.id)
        back = back.data
        if (back.resultCode === '200') {
          this.form.name = back.result.name
          this.form.description = back.result.description
        } else {
          this.$alert(back.msg, '提示')
        }
      } catch (err) {
        this.$alert('请求出错', '提示')
      }
    },
    async edit() {
      try {
        let back = await reviseRole(this.id, this.form)
        back = back.data
        if (back.resultCode === '200') {
          this.$emit('update')
          this.close()
        } else {
          this.$alert(back.msg, '提示')
        }
      } catch (err) {
        this.$alert('请求出错', '提示')
      }
    }
  }
}
</script>
<style lang="scss" scoped>

</style>
